package ase.eleitweg.wfeditor.gui;


import java.util.Iterator;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ase.eleitweg.common.Edge;
import ase.eleitweg.common.Node;
import ase.eleitweg.wfeditor.gui.GDesigner.INode;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class GDCreateEdge {

	
	private Text beschreibung;
	private Combo toCombo;
	private Combo fromCombo;
	private Mode mode;
	private Node node;
	
	public enum Mode{ CREATE, HALFNEW };
	
	private Edge edge = null;
	
	public GDCreateEdge(){
		this(Mode.CREATE,null);
	}
	
	public GDCreateEdge(Mode mode,Node node){
		this.mode = mode;
		if(mode == Mode.HALFNEW) this.node = node;
		else node = null;
	}
	
	/**
	 * Open the window
	 */
	public Edge open(VisualizationViewer vv) {
		final Display display = Display.getDefault();
		final Shell shell = new Shell();
		shell.setSize(253, 166);
		shell.setText("SWT Application");

		shell.open();

		final Set verticesSet = vv.getGraphLayout().getGraph().getVertices();
		
		fromCombo = new Combo(shell, SWT.NONE);
		fromCombo.setBounds(111, 10, 125, 21);
		if(mode == Mode.HALFNEW) fromCombo.setEnabled(false);
		
		toCombo = new Combo(shell, SWT.NONE);
		toCombo.setBounds(111, 37, 125, 21);

		for(Iterator iterator = verticesSet.iterator(); iterator.hasNext();){
			INode tmpNode = (INode)iterator.next();
			String tmpNodeName = tmpNode.getNode().getName();
			fromCombo.add(tmpNodeName);
			toCombo.add(tmpNodeName);
		}
		
		if(mode == Mode.HALFNEW){
			boolean found = false;
			int i = 0,k = 0;
			String[] tmpArr = fromCombo.getItems();
			for(String tmpStr: tmpArr){
				i++;
				if(tmpStr.equalsIgnoreCase(node.getName())){
					found = true;
					k=i-1;
				}
			}
			if(found) fromCombo.select(k);
		}

		final Button createEdgeButton = new Button(shell, SWT.NONE);
		createEdgeButton.setText("OK");
		createEdgeButton.setBounds(54, 110, 90, 22);
		createEdgeButton.addMouseListener(new MouseAdapter(){
			public void mouseDown(MouseEvent event){
				if(toCombo.getSelectionIndex()>-1 && fromCombo.getSelectionIndex()>-1){
					Node fromNode = null, toNode = null;
					
					for(Iterator iterator = verticesSet.iterator(); iterator.hasNext();){
						Node tmpNode = ((INode)iterator.next()).getNode();
						
						if(toCombo.getItem(toCombo.getSelectionIndex()).equals(tmpNode.getName())){
							toNode = tmpNode;
							break;
						}
					}
					
					for(Iterator iterator = verticesSet.iterator(); iterator.hasNext();){
						Node tmpNode = ((INode)iterator.next()).getNode();
						
						if(fromCombo.getItem(fromCombo.getSelectionIndex()).equals(tmpNode.getName())){
							fromNode = tmpNode;
							break;
						}
					}
					
					if(fromNode==null) System.out.println("GDCreateEdge: FromNode is null");
					if(toNode==null) System.out.println("GDCreateEdge: ToNode is null");
					if(fromNode!=null && toNode!=null){
						System.out.println(beschreibung.getText());
						edge = new Edge(beschreibung.getText(), fromNode, toNode);
					}
					else edge=null;
				}
				if(edge==null){
					MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
					box.setText("Fehler");
					box.setMessage("Bitte w\u00E4hlen sie Ziel und Endknoten aus!");
					box.open();
				}
				else{
					shell.dispose();
				}
			}
		});

		final Button cancelButton = new Button(shell, SWT.NONE);
		cancelButton.setText("Abbrechen");
		cancelButton.setBounds(150, 110, 90, 22);
		cancelButton.addMouseListener(new MouseAdapter(){
			public void mouseDown(MouseEvent event){
				shell.dispose();
			}
		});

		final Label vonLabel = new Label(shell, SWT.NONE);
		vonLabel.setText("Von:");
		vonLabel.setBounds(10, 10, 90, 21);

		final Label zuLabel = new Label(shell, SWT.NONE);
		zuLabel.setText("Zu:");
		zuLabel.setBounds(10, 37, 90, 21);

		final Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setBounds(10, 91, 226, 13);

		final Label beschreibungLabel = new Label(shell, SWT.NONE);
		beschreibungLabel.setText("Beschreibung:");
		beschreibungLabel.setBounds(10, 64, 90, 21);

		beschreibung = new Text(shell, SWT.BORDER);
		beschreibung.setBounds(111, 61, 125, 21);
		shell.layout();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		return edge;
	}
}
